//
//  ChangeLoveSignViewController.m
//  CommunityApp
//
//  Created by 秦乾龙 on 16/12/14.
//  Copyright © 2016年 秦乾龙. All rights reserved.
//

#import "ChangeLoveSignViewController.h"
#import "AFNetworking.h"//主要用于网络请求方法
#import "UIKit+AFNetworking.h"//里面有异步加载图片的方法
#import "AFHTTPSessionManager.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
#import "NavView.h"
#import "ZYButton.h"
#import "TagSign.h"
#import "ZYTableViewCell.h"
#import "AddLoveSignViewController.h"
#import "ChangePersonSignViewController.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScale   ([UIScreen mainScreen].bounds.size.width)/750
@interface ChangeLoveSignViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain) UITableView *mainTableView;
@property (nonatomic,retain) NSMutableArray *tagSignArr;

@end

@implementation ChangeLoveSignViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"添加标签"];
    
    UIBarButtonItem *backBtn = [ViewFactory makeNavigationBackWhite:self action:@selector(backClick)];
    self.navigationItem.leftBarButtonItem = backBtn;
    
    UIButton *buttonNotice = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonNotice.frame = CGRectMake(0, 0, 40, 40);
    buttonNotice.titleLabel.font = [UIFont systemFontOfSize:15];
    [buttonNotice setTitle:@"添加" forState:UIControlStateNormal];
    UIBarButtonItem *btnNoticeItem = [[UIBarButtonItem alloc]initWithCustomView:buttonNotice];
    [buttonNotice addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = btnNoticeItem;
    
    
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,kScreenW , kScreenH-64) style:UITableViewStyleGrouped];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    [self.view addSubview:_mainTableView];
    [self request];
    
}

- (void)request
{
    NSString *urlStr = [NSString stringWithFormat:@"%@/userCenter/userTagListByUserID",APIYIGOU];
    DYBaseRequest *request = [DYBaseRequest new];
    request.requestUrl = urlStr;
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *allDic = [NSDictionary changeType:request.responseJSONObject];
        NSLog(@"77777%@",allDic);
        _tagSignArr = allDic[@"data"];
        [_mainTableView reloadData];
    } error:^(YTKBaseRequest *request) {
        
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tagSignArr.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"cell";
    ZYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell =[[NSBundle mainBundle ]loadNibNamed:@"ZYTableViewCell" owner:nil options:nil][0];
        
    }
    
    TagSign *tagSign = [[TagSign alloc]initWithDic:_tagSignArr[indexPath.row]];
    UILabel *signNameLable = [[UILabel alloc]initWithFrame:CGRectMake(30*kScale, 29*kScale, kScreenW-60*kScale,42*kScale)];
    signNameLable.text = tagSign.name;
    [cell addSubview:signNameLable];
    ZYButton *removeBtn = [ZYButton buttonWithType:UIButtonTypeCustom];
    removeBtn.frame = CGRectMake(kScreenW, 0, 200*kScale, 100*kScale);
    removeBtn.backgroundColor = [UIColor colorWithRed:15/255.0 green:162/255.0 blue:182/255.0 alpha:1.0f];
    [removeBtn setTitle:@"删除" forState:UIControlStateNormal];
    [removeBtn addTarget:self action:@selector(removeClick:) forControlEvents:UIControlEventTouchUpInside];
    removeBtn.tagSign = tagSign;
    [cell addSubview:removeBtn];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  100*kScale;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //        [dataArray removeObjectAtIndex:indexPath.row];
        // Delete the row from the data source.
        //        [_mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        //        [_mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}
-(NSArray<UITableViewRowAction*>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
                                                                         title:@"              " handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                                                                             
                                                                             
                                                  TagSign *tagSign = [[TagSign alloc]initWithDic:_tagSignArr[indexPath.row]];
                                                                             NSString *urlStr = [NSString stringWithFormat:@"%@/userCenter/delectMyTagByUserId?id=%@",APIYIGOU,tagSign.tagId];
                                                                             DYBaseRequest *request = [DYBaseRequest new];
                                                                             request.requestUrl = urlStr;
                                                                             [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
                                                                                 NSDictionary *allDic = [NSDictionary changeType:request.responseJSONObject];
                                                                                 NSLog(@"77777%@",allDic);
                                                                                 
                                                 [_tagSignArr removeObjectAtIndex:indexPath.row];                                [_mainTableView reloadData];
                                                                             } error:^(YTKBaseRequest *request) {
                                                                                 
                                                                             } failure:^(YTKBaseRequest *request) {
                                                                                 [ViewFactory showErrorStrMsg:NETWORK_ERR];
                                                                             }];
                            
                                                                         }];
    
    //    UITableViewRowAction *rowActionSec = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
    //                                                                            title:@"快速备忘"    handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
    //                                                                                NSLog(@"快速备忘");
    //
    //                                                                            }];
    //    rowActionSec.backgroundColor = [UIColor cyanColor];
    rowAction.backgroundColor = [UIColor colorWithRed:15/255.0 green:162/255.0 blue:182/255.0 alpha:1.0f];
    
    
    NSArray *arr = @[rowAction];
    return arr;
}
- (void)addClick
{
    AddLoveSignViewController *add = [[AddLoveSignViewController alloc]init];
    [self.navigationController pushViewController:add animated:YES];
}
- (void)removeClick:(ZYButton*)sender
{
    [_tagSignArr removeObject:sender.tagSign];
    [sender.cellView removeFromSuperview];
}
- (void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
